package com.uu.it.tracker.gps;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class Gps extends Service {

	private Location preLocation;
	
	private LocationManager locationManager;
	/**
	 * distance in meters between the previous location
	 * and current location
	 */
	private static float distance;
	/**
	 * speed at the time location is updated, m/s
	 */
	private static float speed;
		   
    @Override
	 public void onCreate(){
		 super.onCreate();
		 Log.i("gps","gpsCreate()");
		
	 }
    
    GpsStatus.Listener gpsListener = new GpsStatus.Listener() {
		
		public void onGpsStatusChanged(int event) {
			// TODO Auto-generated method stub
			switch (event){
			case GpsStatus.GPS_EVENT_STARTED:
				Toast.makeText(Gps.this, "Gps Status Start", Toast.LENGTH_SHORT).show();
				break;
			case GpsStatus.GPS_EVENT_STOPPED:
				Toast.makeText(Gps.this, "Gps Status Stop", Toast.LENGTH_SHORT).show();
			}
		}
	};
    @Override
    public void onStart(Intent intent, int startId){
    	super.onStart(intent, startId);
    	Log.i("gps","gpsStart()");
    	
		    String context = Context.LOCATION_SERVICE;
		    locationManager = (LocationManager)getSystemService(context); 
		    locationManager.addGpsStatusListener(gpsListener);
		    
		    Criteria criteria = new Criteria();
		    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		    criteria.setAltitudeRequired(false);
		    criteria.setBearingRequired(false);
		    criteria.setCostAllowed(false);
		    criteria.setPowerRequirement(Criteria.POWER_LOW);
		    
		    String provider = locationManager.getBestProvider(criteria, true);
		    
		    preLocation = locationManager.getLastKnownLocation(provider);
		    
		    updateWithNewLocation(preLocation);

		    locationManager.requestLocationUpdates(provider, 2000, 0,
		                                           locationListener);
    }
    
	private void updateWithNewLocation(Location location) {
		// TODO Auto-generated method stub
		Log.i("gps","update with new location");
		if (location != null){
			distance = location.distanceTo(preLocation);
			speed = location.getSpeed();
			Log.i("gps speed", ""+speed);
			Log.i("gps distance",""+distance);
			preLocation = location;
		}
		
	}

	@Override
	public void onDestroy(){
		locationManager.removeUpdates(locationListener);
		super.onDestroy();
		
		Log.i("gps", "gpsDestroy");
		
	}
	private final LocationListener locationListener = new LocationListener() {

		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub
			 updateWithNewLocation(arg0);

		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			 updateWithNewLocation(null);

		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			switch (status){
			case LocationProvider.AVAILABLE:
				Toast.makeText(Gps.this, "Location Provider Available", Toast.LENGTH_SHORT).show();
				break;
			}
		}
		
	};

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		Log.i("gps", "gpsOnBind");
		return null;
	}

}
